package saas.adapter.sdk.init;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import saas.adapter.sdk.callback.ConsumeMessageHookCallback;
import saas.adapter.sdk.callback.SendMessageHookCallback;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @title: HookLocator
 * @description:
 * @author: guan.xin
 * @create: 2021-11-25 09:47
 * @since: JDK8
 */
@Component
public class HookLocator implements ApplicationContextAware {

    private final Map<String, ConsumeMessageHookCallback> consumeMessageHookMap = new HashMap<>();

    private final Map<String, SendMessageHookCallback> sendMessageHookMap = new HashMap<>();

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        Map<String, ConsumeMessageHookCallback> map1 = applicationContext.getBeansOfType(ConsumeMessageHookCallback.class);
        if (!map1.isEmpty()) {
            consumeMessageHookMap.putAll(map1);
        }

        Map<String, SendMessageHookCallback> map2 = applicationContext.getBeansOfType(SendMessageHookCallback.class);
        if (!map2.isEmpty()) {
            sendMessageHookMap.putAll(map2);
        }
    }

    public List<ConsumeMessageHookCallback> getConsumeMessageHookList() {
        return new ArrayList<>(consumeMessageHookMap.values());
    }

    public List<SendMessageHookCallback> getSendMessageHookList() {
        return new ArrayList<>(sendMessageHookMap.values());
    }
}
